import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../bloc/MessageSendCubit.dart';
import '../../../ext/AudioSelectionControls.dart';
import '../MessagePaneBackgroundView.dart';
import '../UiUtil.dart';
import 'ImMessageHandler.dart';
import 'MessagePane.dart';

class AudioMessagePane extends StatelessWidget {
  final WrapImMessage wrapMessage;

  const AudioMessagePane({required this.wrapMessage, super.key});

  @override
  Widget build(BuildContext context) {
    return _AudioMessagePane(message: wrapMessage.message);
  }
}

class AudioMessageRefPane extends _AudioMessagePane {

  final MessageRefParam param;

  const AudioMessageRefPane({required super.message, required this.param, super.key});

}

class _AudioMessagePane extends StatelessWidget {

  final ImMessage message;

  const _AudioMessagePane({required this.message, super.key});

  @override
  Widget build(BuildContext context) {
    if (message.messageExt == null) {
      message.initMessageExt();
    }
    var time = ((message.messageExt?.time??0)/1000).toStringAsFixed(1) + "s";
    return _buildBackgroundPane(context, time);
  }

  Widget _buildBackgroundPane(BuildContext context, String time) {

    return MessagePaneBackgroundView(
        message: message,
        child: SelectableText.rich(_buildPane(time),
          onTap: () async => playAudio(context),
        )
    );
  }

  TextSpan _buildPane(String time) {
    return _buildLeftPane(time);
  }

  TextSpan _buildLeftPane(String time) {
    return TextSpan(children: [
      WidgetSpan(child: buildSvgView('assets/svg/wave_right.svg', 20, 20, Colors.black54)),
      TextSpan(text: time),
    ],);
  }

  TextSpan _buildRightPane(String time) {
    return TextSpan(children: [
      TextSpan(text: time),
      WidgetSpan(child: buildSvgView('assets/svg/wave_left.svg', 20, 20, Colors.black54))
    ],);
  }

  void playAudio(BuildContext context) async {
    var messageHandler = ImMessageHandler();
    messageHandler.playAudio(context, message);

  }
}
